#!/usr/bin/even python

import cv2
import tkinter.messagebox

W = 640 # 视频宽度
H = 480 # 视频高度

# def zh_ch(string):
#   return string.encode("utf-8").decode(errors="ignore")

if __name__ == "__main__":
  try:
    capture = cv2.VideoCapture(0)
    capture_usb = cv2.VideoCapture(1)
    capture_usb1 = cv2.VideoCapture(2)
    capture_usb2 = cv2.VideoCapture(3)
  except:
    tkinter.messagebox.showinfo('摄像头打开失败')

  # 打开自带的摄像头
  if capture.isOpened():
    try:
      # 以下设置显示屏的宽高
      capture.set(cv2.CAP_PROP_FRAME_WIDTH, W)
      capture.set(cv2.CAP_PROP_FRAME_HEIGHT, H)
    except:
      tkinter.messagebox.showinfo('摄像头1打开异常，请确认正确安装')

  if capture_usb.isOpened():
    try:
      # 以下设置显示屏的宽高
      capture_usb.set(cv2.CAP_PROP_FRAME_WIDTH, W)
      capture_usb.set(cv2.CAP_PROP_FRAME_HEIGHT, H)
    except:
      tkinter.messagebox.showinfo('摄像头2打开异常，请确认正确安装')

  if capture_usb1.isOpened():
    try:
      # 以下设置显示屏的宽高
      capture_usb1.set(cv2.CAP_PROP_FRAME_WIDTH, W)
      capture_usb1.set(cv2.CAP_PROP_FRAME_HEIGHT, H)
    except:
      tkinter.messagebox.showinfo('摄像头3打开异常，请确认正确安装')
  
  if capture_usb2.isOpened():
    try:
      # 以下设置显示屏的宽高
      capture_usb2.set(cv2.CAP_PROP_FRAME_WIDTH, W)
      capture_usb2.set(cv2.CAP_PROP_FRAME_HEIGHT, H)
    except:
      tkinter.messagebox.showinfo('摄像头3打开异常，请确认正确安装')
    
  # 持续读取摄像头数据
  c1flag = True
  c2flag = True
  c3flag = True
  c4flag = True
  while True:
      read_code, frame = capture.read()
      read_code2, frame2 = capture_usb.read()
      read_code3, frame3 = capture_usb1.read()
      read_code4, frame4 = capture_usb2.read()
      if not read_code or not read_code2 or not read_code3 or not read_code4:
          break
      if c1flag:
        cv2.imshow('camera    V0.1      by: ChenKaiFeng', frame)
      if c2flag:
        cv2.imshow('camera_usb   V0.1      by: ChenKaiFeng', frame2)
      if c3flag:
        cv2.imshow('camera_usb1   V0.1      by: ChenKaiFeng', frame3)
      if c4flag:
        cv2.imshow('camera_usb2   V0.1      by: ChenKaiFeng', frame4)
      # 输入 q 键，保存当前画面为图片
      if cv2.waitKey(1) == ord('q'):
          # 设置图片分辨率
          frame = cv2.resize(frame, (1920, 1080))
          cv2.imwrite('pic.jpg', frame)
          capture_usb.release()
          break
      # print(cv2.waitKey(27))
      
      # 关闭独立窗口
      # if cv2.getWindowProperty('camera    V0.1      by: ChenKaiFeng', cv2.WND_PROP_VISIBLE) < 1:
      #   c1flag = False
      #   capture.release()
      #   cv2.destroyWindow("camera    V0.1      by: ChenKaiFeng")

      # if cv2.getWindowProperty('camera_usb   V0.1      by: ChenKaiFeng', cv2.WND_PROP_VISIBLE) < 1:
      #   c2flag = False
      #   capture_usb.release()
      #   cv2.destroyWindow("camera_usb   V0.1      by: ChenKaiFeng")

      # if cv2.getWindowProperty('camera_usb1   V0.1      by: ChenKaiFeng', cv2.WND_PROP_VISIBLE) < 1:
      #   c3flag = False
      #   capture_usb1.release()
      #   cv2.destroyWindow("camera_usb1   V0.1      by: ChenKaiFeng")

      # if cv2.getWindowProperty('camera_usb2   V0.1      by: ChenKaiFeng', cv2.WND_PROP_VISIBLE) < 1:
      #   c4flag = False
      #   capture_usb1.release()
      #   cv2.destroyWindow("camera_usb2   V0.1      by: ChenKaiFeng")
  
      if cv2.waitKey(27) == 27:
      #   print('press ESC')
        # 释放资源  
        capture.release()
        capture_usb.release()
        capture_usb1.release()
        capture_usb2.release()
        cv2.destroyWindow("camera    V0.1      by: ChenKaiFeng")
        cv2.destroyWindow("camera_usb   V0.1      by: ChenKaiFeng")
        cv2.destroyWindow("camera_usb1   V0.1      by: ChenKaiFeng")
        cv2.destroyWindow("camera_usb2   V0.1      by: ChenKaiFeng")